package cn.stylefeng.roses.kernel.conversion.util;

import com.spire.doc.*;
import com.spire.doc.documents.ImageType;
import com.spire.doc.documents.Paragraph;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * doc文件操作工具类
 *
 * @author fengshuonan
 * @date 2021/8/26 14:20
 */
public class DocUtil {

    /**
     * doc转化为pdf
     *
     * @param inputFile  原有doc或docx文件路径
     * @param outputFile 输出的pdf文件路径
     * @author fengshuonan
     * @date 2021/8/26 14:35
     */
    public static void toPdf(String inputFile, String outputFile) {
        Document document = new Document();
        document.loadFromFile(inputFile);
        document.saveToFile(outputFile, FileFormat.PDF);
    }

    /**
     * doc转化为pdf，读入字节，输出到流中
     *
     * @param inputFileBytes 原有doc或docx文件字节数组
     * @author fengshuonan
     * @since 2023/10/20 22:48
     */
    public static void toPdfByBytes(byte[] inputFileBytes, String suffix, OutputStream outputStream) {
        Document document = new Document();

        if (suffix.contains("docx")) {
            document.loadFromStream(new ByteArrayInputStream(inputFileBytes), FileFormat.Docx);
        } else {
            document.loadFromStream(new ByteArrayInputStream(inputFileBytes), FileFormat.Doc);
        }

        document.saveToStream(outputStream, FileFormat.PDF);
    }

    /**
     * doc转化为pdf（禁用超链接）
     *
     * @param inputFile  原有doc或docx文件路径
     * @param outputFile 输出的pdf文件路径
     * @author fengshuonan
     * @date 2021/8/26 14:37
     */
    public static void toPdfDisableHyperlinks(String inputFile, String outputFile) {
        Document document = new Document();
        document.loadFromFile(inputFile);

        ToPdfParameterList pdf = new ToPdfParameterList();
        pdf.setDisableLink(true);
        document.saveToFile(outputFile, pdf);
    }

    /**
     * doc转化为pdf（嵌入式字体）
     *
     * @param inputFile  原有doc或docx文件路径
     * @param outputFile 输出的pdf文件路径
     * @author fengshuonan
     * @date 2021/8/26 14:37
     */
    public static void toPdfEmbedAllFontsInPDF(String inputFile, String outputFile) {
        Document document = new Document();
        document.loadFromFile(inputFile);

        //embeds full fonts by default when IsEmbeddedAllFonts is set to true.
        ToPdfParameterList ppl = new ToPdfParameterList();
        ppl.isEmbeddedAllFonts(true);

        document.saveToFile(outputFile, ppl);
    }

    /**
     * doc转化为pdf（嵌入式字体）
     *
     * @param inputFile  原有doc或docx文件路径
     * @param outputFile 输出的pdf文件路径
     * @author fengshuonan
     * @date 2021/8/26 14:37
     */
    public static void toPdfEmbedNonInstalledFonts(String inputFile, String outputFile, String fontFile) {
        Document document = new Document();
        document.loadFromFile(inputFile);

        //embed the non-installed fonts.
        ToPdfParameterList parms = new ToPdfParameterList();
        List<PrivateFontPath> fonts = new ArrayList<>();
        fonts.add(new PrivateFontPath("PT Serif Caption", fontFile));
        parms.setPrivateFontPaths(fonts);

        //save the document to a PDF file.
        document.saveToFile(outputFile, parms);
    }

    /**
     * doc转化为pdf（设置图片的质量）
     *
     * @param inputFile  原有doc或docx文件路径
     * @param outputFile 输出的pdf文件路径
     * @param quality    默认是80%的图片质量，这里可以输入图片质量百分比
     * @author fengshuonan
     * @date 2021/8/26 14:37
     */
    public static void toPdfSetImgQuality(String inputFile, String outputFile, int quality) {
        Document document = new Document();
        document.loadFromFile(inputFile);
        document.setJPEGQuality(quality);
        document.saveToFile(outputFile, FileFormat.PDF);
    }

    /**
     * word转图片
     *
     * @author fengshuonan
     * @date 2021/8/26 14:47
     */
    public static void toImage(String inputFile, String outputFile) throws IOException {
        //创建Document对象
        Document doc = new Document();

        //加载Word文档
        doc.loadFromFile(inputFile);

        //将指定页保存为BufferedImage
        BufferedImage[] bufferedImages = doc.saveToImages(ImageType.Bitmap);

        //将图片数据保存为PNG格式文档
        for (int i = 1; i <= bufferedImages.length; i++) {
            File file = new File(outputFile + "." + i + ".png");
            ImageIO.write(bufferedImages[i - 1], "PNG", file);
        }
    }

    /**
     * 往docx文件中写一段文字
     *
     * @author fengshuonan
     * @date 2021/8/26 14:22
     */
    public static void writeText(String content, String filePath) {
        Document document = new Document();
        Section section = document.addSection();
        Paragraph paragraph = section.addParagraph();
        paragraph.appendText(content);
        document.saveToFile(filePath, FileFormat.Docx);
    }

}
